//验证手机号是否已经注册过
$("#userphone").blur(function () {
    chackTelnoFormat();//js检查
    checkTelnoValide();//后端数据检查

});

function checkTelnoValide() {
    var telno = $("#userphone").val()// 获得手机号
    // 发送ajax请求到后端 验证该手机号是否存在
    $.ajax({
        type:"GET",
        url:"/customer/telno/"+telno,
        datatype:"json",
        success:function (result) {
            console.log(result);
            if (result.status == 1){
                //用户已经注册过
                $("#shoujihao").text(result.data)
                $("#shoujihao").css("display","block")
            }else {
                //尚未注册
                $(".shoujihao").text("")
                console.log("用户尚未注册:  "+result.data);
            }
        }
    })
}

var interValObj ;// 计时器变量，控制时间
var count = 60; // 间隔函数
var curCount ;//当前剩余秒数

//2. 获得按钮点击，点击后，按钮内部显示 倒计时
$(".send-information").click(function () {
    curCount = count
    $(".send-information").attr("disabled","true")
    $(".send-information").val(curCount+"秒后可以重新发送")
    $(".send-information").css("font-size","12px")
    interValObj = window.setInterval(setRemainTime,1000)
})


//计时器
function setRemainTime() {
    if (curCount == 0){
        window.clearInterval(interValObj)
        $(".send-information").removeAttr("disabled")
        $(".send-information").val("重新发送验证码")
    }else {
        curCount--
        $(".send-information").val(curCount + "秒后可以重新发送");
    }
}

var serverCode ;//服务器中的而短信验证码

//  获得页面手机号，ajax请求到后端先检查手机号，若没被注册过，获得验证码，服务器返回验证码
$(".send-information").click(function () {
    var telno = $("#userphone").val()// 获得手机号
    $.ajax({
        type:"post",
        url:"/customer/code",
        data:"telno="+telno,
        datatype:"json",
        success:function (result) {
            console.log(result)
           //??????????
            if(result.status ==2){//成功调用
                //用户输入验证码
                serverCode = result.data;//后端返回的验证码
            }else{//调用失败
                $(".shoujihao").text(result.data);
                window.clearInterval(interValObj);
                $(".send-information").removeAttr("disabled");
                $(".send-information").val("重新发送验证码");
            }
        }
    })
})

//带着验证码与表单中其他数据进行注册
$(".register-btn").click(function () {
   var inputCode = $(".VerificationCode").val()
    if (inputCode == serverCode) {
        //ajax 请求注册用户 （添加数据到数据库中）
        console.log("ajax 请求注册用户 （添加数据到数据库中）");
        $(".error-font").css("display","none")
        regist();
       /* var isAgree = $(".isAgree")
        if (isAgree.checked != null){
            regist();
        }*/

    }else {
        //用户输入的验证码错误
        $(".error-msg").text("用户输入的验证码错误,请重新输入");
        $(".error-font").css("display","block")
    }
})


//4.注册业务：就是添加功能
function regist() {
    var formdata = $("#regist_from").serialize()
    $.ajax({
        type:"POST",
        url:"/customer/add",
        data: formdata,
        datatype:"json",
        success:function (result) {
            console.log(result);
            if (result.status == 0){
                //添加失败
                console.log("注册失败");
            }else {
                console.log("注册成功");
                window.location.href="/index";
            }
        }

    })
}




//切换手机注册和密码注册
document.querySelector(".new-kind").onclick = function () {
    if (document.querySelector("#regist_ByTelno").style.display == "none") {


        document.querySelector("#regist_ByTelno").style.display = "block";
        document.querySelector("#regist_ByEmail").style.display = "none";
        document.querySelector(".now-kind").innerText = "手机注册";
        document.querySelector(".new-kind").innerText = "邮箱注册";
    } else {
        document.querySelector("#regist_ByTelno").style.display = "none";
        document.querySelector("#regist_ByEmail").style.display = "block";
        document.querySelector(".now-kind").innerText = "邮箱注册";
        document.querySelector(".new-kind").innerText = "手机注册";
    }
}

//验证手机号码输入是否正确
function chackTelnoFormat() {
    var usertelno = $("#userphone").val()
    $("#shoujihao").html("")
    var ele
    if (usertelno.trim() ==null || usertelno.trim() =="") {
        ele = "<span class=\"error_logo\"></span>\n            " +
            "                          <span>请输入手机号</span>"
        $("#shoujihao").html(ele)
        $("#shoujihao").css("display","block")
    }else if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(usertelno)){
        ele ="<span class=\"error_logo\"></span>\n               " +
            "                         <span>当前地区或国家手机号码不正确，请重新输入</span>"
        $("#shoujihao").html(ele)
        $("#shoujihao").css("display","block")

    }else{
        $("#shoujihao").html("")
        $("#shoujihao").css("display","none")

    }
}



//获得密码
    var usepwdEle = document.querySelector(".userPassword");
usepwdEle.onblur = function(){
    var usepwd = usepwdEle.value;
    var userpwdErrorEle = document.querySelector(".mima")
    userpwdErrorEle.innerHTML = ''
    if (usepwd.trim() == null || usepwd.trim() == "") {
        userpwdErrorEle.innerHTML ="<span class=\"error_logo\"></span>\n                " +
            "        <span>请输入密码</span>";
        userpwdErrorEle.style.display = "inline";
        return false;
    } else if (!/^[0-9A-Za-z]{6,12}$/.test(usepwd)) {
        userpwdErrorEle.innerHTML = "<span class=\"error_logo\"></span>\n              " +
            "          <span>用户名长度在6-12个字符，由数字与字母组成</span>"
        userpwdErrorEle.style.display = "inline";
        return false;
    } else {
        userpwdErrorEle.innerHTML = "";
        userpwdErrorEle.style.display = "none";
        return true;
    }
}

//确认密码
var confirmPassword = document.querySelector(".confirmPassword");
confirmPassword.onblur = function(){
    var usepwd = document.querySelector(".userPassword").value;
    var confirmpwd = confirmPassword.value;
    var conFirmError = document.querySelector(".conFirmError")
    conFirmError.innerHTML = ''
    if (confirmpwd.trim() == null || confirmpwd.trim() == "") {
        conFirmError.innerHTML ="<span class=\"error_logo\"></span>\n                " +
            "        <span>请再次输入密码</span>";
        conFirmError.style.display = "inline";
        return false;
    } else if (usepwd != confirmpwd) {
        conFirmError.innerHTML = "<span class=\"error_logo\"></span>\n              " +
            "          <span>请输入与上面密码相同</span>"
        conFirmError.style.display = "inline";
        return false;
    } else {
        conFirmError.innerHTML = "";
        conFirmError.style.display = "none";
        return true;
    }
}




/*****************************邮箱注册***********************************
 ***************************************************************/



